package com.fenghuajueli.idiomppp.database;

import android.content.Context;

import com.fenghuajueli.idiomppp.entity.IdiomPinDbEntity;
import com.fenghuajueli.idiomppp.entity.IdiomPinEntity;

import java.util.List;

/**
 * 短语数据表的操作
 */
public class IdiomPinManager {
    private DaoManager manager;

    public IdiomPinManager(Context context) {
        manager = DaoManager.getInstance();
        manager.init(context);
    }

    /**
     * 完成对数据库中短语 表的插入操作
     *
     * @param idiomPinEntity
     * @return flag
     */
    public boolean insert(IdiomPinDbEntity idiomPinEntity) {
        boolean flag = manager.getDaoSession().insertOrReplace(idiomPinEntity) != -1 ? true : false;
        return flag;
    }

    /**
     * 插入多条记录，需要开辟新的线程
     *
     * @param idiomPinEntities
     * @return flag
     **/
    public boolean insert(final List<IdiomPinDbEntity> idiomPinEntities) {
        boolean flag = false;
        try {
            manager.getDaoSession().runInTx(() -> {
                for (IdiomPinDbEntity IdiomPinEntity : idiomPinEntities) {
                    manager.getDaoSession().insertOrReplace(IdiomPinEntity);
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 完成对短语表的某一条记录的修改
     *
     * @param idiomPinEntity
     * @return flag
     */
    public boolean updateDuanYu(IdiomPinDbEntity idiomPinEntity) {
        boolean flag = false;
        try {
            manager.getDaoSession().update(idiomPinEntity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @param IdiomPinEntity
     * @return flag
     */
    public boolean deleteDuanYu(IdiomPinDbEntity IdiomPinEntity) {
        boolean flag = false;
        try {
            manager.getDaoSession().delete(IdiomPinEntity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     *
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 返回多行记录 * * @return
     */
    public List<IdiomPinDbEntity> listAll() {
        return manager.getDaoSession().loadAll(IdiomPinDbEntity.class);
    }

    /**
     * 按照主键返回单行记录 * * @param key * @return
     */
    public IdiomPinDbEntity getOneData(long key) {
        return manager.getDaoSession().load(IdiomPinDbEntity.class, key);
    }

    /**
     * 查询短语表中所有数据
     *
     * @return List<DbBookshelf>
     */
    public List<IdiomPinDbEntity> queryAll() {
        List<IdiomPinDbEntity> list = manager.getDaoSession().loadAll(IdiomPinDbEntity.class);
        return list;
    }


}
